package com.mb;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.apache.log4j.Logger;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;

import com.common.util.messages.MessagesController;
import com.facade.MembershipFacade;
import com.model.Membership;
import com.model.User;
import com.objects.MembershipSearch;

@ViewScoped
@ManagedBean
public class MembershipMB extends AbstractMB implements Serializable {
	private static final long serialVersionUID = 1L;
	
	private Membership membership;
	private MembershipSearch membershipSearch = new MembershipSearch();
	private List<Membership> memberships;
	private List<Membership> filteredMemberships;
	private List<Membership> activeMemberships;
	
	private static Logger log = Logger.getLogger(MembershipMB.class);
	
	private MembershipFacade membershipFacade; 
	
	MessagesController msgCtrl = new MessagesController();
	
	public void createMembershipPackage() {
		try {
			UserMB userMb = new UserMB();
			User user = userMb.getLoggedUser();

			membership.setCreatedDate(new Date());
			membership.setCreatedBy(user);
			membership.setUpdatedDate(new Date());
			membership.setUpdatedBy(user);
			membership.setActive(true);
			
			getMembershipFacade().createMembership(membership);
			closeDialog();
			msgCtrl.addInfo("New Membership Plan Saved", membership.getName() + " is saved successfully");
			resetMembership();
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			log.error("Membership Plan error in saving");
		}
	}
	
	public void updateMembership() {
		try {
			UserMB userMb = new UserMB();
			User user = userMb.getLoggedUser();
			
			membership.setUpdatedDate(new Date());
			membership.setUpdatedBy(user);
			
			getMembershipFacade().updateMembership(membership);
			closeDialog();
			msgCtrl.addInfo("Membership Info Updated", membership.getName() + " is updated successfully");
			loadMemberships();
			loadActiveMemberships();
			resetMembership();
		} catch (Exception e) {
			keepDialogOpen();
			msgCtrl.addFatal("Uhh Ohh", "something is not right here.");
			log.error("Membership Plan error in updating");
		}
	}	
	
	public void search(){
		memberships = getMembershipFacade().search(membershipSearch);
	}	
	
	public MembershipFacade getMembershipFacade() {
		if (membershipFacade == null) {
			membershipFacade = new MembershipFacade();
		}		
		return membershipFacade;
	}
	
	public Membership getMembership() {
		if (membership == null) {
			membership = new Membership();
		}
		return membership;
	}
	
	public void setMembership(Membership membership) {
		this.membership = membership;
	}
	
	public List<Membership> getAllMemberships() {
		if (memberships == null) {
			loadMemberships();
		}

		return memberships;
	}	
	
	private void loadMemberships() {
		memberships = getMembershipFacade().listAll();
	}
	
	public void resetMembership() {
		membership = new Membership();
	}
	
	public List<Membership> getFilteredMemberships() {
		return filteredMemberships;
	}

	public void setFilteredMemberships(List<Membership> filteredMemberships) {
		this.filteredMemberships = filteredMemberships;
	}	
	
    public void onRowSelect(SelectEvent event) { 
    }  
    
    public void onRowUnselect(UnselectEvent event) {  

    }
    
	public void preSelectedMembership(Membership selectedMembership){
		FacesContext.getCurrentInstance().getExternalContext().getApplicationMap().put("validatorMembershipUpdate", selectedMembership);
	}

	public List<Membership> getActiveMemberships() {
		if(activeMemberships == null){
			loadActiveMemberships();
		}
		
		return activeMemberships;
	}

	public void setActiveMemberships(List<Membership> activeMemberships) {
		this.activeMemberships = activeMemberships;
	}
    
	private void loadActiveMemberships() {
		activeMemberships = getMembershipFacade().listActive();
	}

	public MembershipSearch getMembershipSearch() {
		return membershipSearch;
	}

	public void setMembershipSearch(MembershipSearch membershipSearch) {
		this.membershipSearch = membershipSearch;
	}

	
	
}
